package com.cyf.util;


/**
 * @author hfy
 * @date 2018/11/23
 */
public class TableNameUtil {

    public static String getTableName(Class clazz) {
        TableName tableName = (TableName) clazz.getAnnotation(TableName.class);
        if (tableName == null) {
            // 没有注解，直接类名转下划线命名
            return humpToUnderline(clazz.getSimpleName());
        }
        return tableName.value();
    }

    /***
     * 驼峰命名转为下划线命名
     *
     * @param para
     *        驼峰命名的字符串
     */

    public static String humpToUnderline(String para) {
        StringBuilder sb = new StringBuilder(para);
        //定位
        int temp = 0;
        for (int i = 0; i < para.length(); i++) {
            if (i != 0 && Character.isUpperCase(para.charAt(i))) {
                sb.insert(i + temp, "_");
                temp += 1;
            }
        }
        return sb.toString().toLowerCase();
    }
}
